package com.cw.jeeyt.service.impl.system;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemAdsBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemAdsDao;
import com.cw.jeeyt.dao.model.system.SystemAdsDo;
import com.cw.jeeyt.service.converter.system.SystemAdsConverter;
import com.cw.jeeyt.service.intf.system.SystemAdsService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 轮播广告业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SystemAdsServiceImpl implements SystemAdsService {

    private static final Logger logger = LoggerFactory.getLogger(SystemAdsServiceImpl.class);
    @Autowired
    private SystemAdsDao systemAdsDao;//轮播广告数据库操作接口

    /**
     * 查询所有轮播广告
     */
    @Override
    public List<SystemAdsBo> querySystemAdsList() throws Exception {
        return SystemAdsConverter.doList2BoList(systemAdsDao.querySystemAdsList());
    }

    /**
     * 分页查询轮播广告
     *
     * @param systemAdsBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemAdsBo> queryByPage(SystemAdsBo systemAdsBo, Integer startNum, Integer pageSize)
    throws Exception {
        PageInfo<SystemAdsDo> pageInfo = systemAdsDao.queryByPage(SystemAdsConverter.bo2Do(systemAdsBo), startNum,
            pageSize);
        return new PageInfo<>(pageInfo, SystemAdsConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻轮播广告信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemAdsBo getSystemAdsById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }

        return SystemAdsConverter.do2Bo(systemAdsDao.getById(id));
    }

    /**
     * 新增轮播广告
     *
     * @param systemAdsBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SystemAdsBo systemAdsBo) throws Exception {

        if (systemAdsBo == null) {
            logger.error("轮播广告为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        SystemAdsDo systemAds = SystemAdsConverter.bo2Do(systemAdsBo);
        systemAds.setId(SequenceTool.nextId());
        systemAds.setCreateBy(userVo.getId());
        systemAds.setCreateDate(new Date());
        systemAds.setUpdateBy(userVo.getId());
        systemAds.setUpdateDate(new Date());
        Integer result = systemAdsDao.insert(systemAds);
        return result;
    }

    /**
     * 更新轮播广告信息
     *
     * @param systemAdsBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SystemAdsBo systemAdsBo) throws Exception {

        if (systemAdsBo == null) {
            logger.error("轮播广告为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        SystemAdsDo systemAds = SystemAdsConverter.bo2Do(systemAdsBo);
        systemAds.setUpdateBy(userVo.getId());
        systemAds.setUpdateDate(new Date());

        systemAdsDao.update(systemAds);
    }

    /**
     * 删除轮播广告
     *
     * @param systemAdsBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SystemAdsBo systemAdsBo) throws Exception {

        if (systemAdsBo == null) {
            logger.error("轮播广告为空");
            return;
        }
        systemAdsDao.delete(SystemAdsConverter.bo2Do(systemAdsBo));
    }
}
